#include <cserver/restapi.h>
#include <boost/log/trivial.hpp>
#include <boost/lexical_cast.hpp>
#include <pqxx/transaction.hxx>
#include <boost/format.hpp>

#include <sstream>

namespace csrv{ namespace webcmd{
	static const char* K_this_command = "api/v1/hydrant/status_sum";

	void hydrant_status_summary(Request& req, pqxx::connection& dbconn, SessionContext& , const std::string& /*cmd */){
		auto query = parseQuery(req.getParam("QUERY_STRING"));
		auto& callback = query["callback"];

		StreamGroup sg(req);
		auto& fout = sg.fout;
		response_header(sg.fout, 200);
		if (!callback.empty()) sg.fout << callback << "(";

#if 1
		fout << boost::format(R"({"func":"hydrant/status_sum",
"args":{},
"ret":{"success":true, "message":"",
"value":)");
#endif

		pqxx::work w(dbconn);
		auto result = w.prepared("hydrant_status_summary").exec();

		fout << result2json(result, 0);

		fout << "}}";
		if (!callback.empty()) sg.fout << ")";
	}
}

static CommandAddHelper k_registerHandler("GET", webcmd::K_this_command, &webcmd::hydrant_status_summary);
}




